local rdac1 = elements.allocate('MY12345', 'FUEL1')
elements.element(rdac1, elements.element(elements.DEFAULT_PT_STNE))
elements.property(rdac1, 'Name', "D"..rdac1)
elements.property(rdac1, "MenuSection", elem.SC_NUCLEAR)
elements.property(rdac1, "Flammable", 0)
elements.property(rdac1, "Description", "")
elements.property(rdac1, "Properties", elem.TYPE_PART+elem.PROP_LIFE_DEC)
elements.property(rdac1, "HighTemperature", 10001)

local tvx = {-1,0,1,0}
local tvy = {0,-1,0,1}

local function rdac1_update(i, x, y, ss, nt)
	local photId = sim.photons(x, y)
	local newPhotId, angle
	if sim.partProperty(i, 9) == 1 then
		if sim.partProperty(i, 1) == 0 then
			if ss > 1 then
				sim.partCreate(i, x, y, 143)
				return
			end
			newPhotId = sim.partCreate(-1, x, y, 173)
			if newPhotId >= 0 then
				angle = math.random()*6.2831853071796
				sim.partProperty(newPhotId, 5, 1.5*math.cos(angle))
				sim.partProperty(newPhotId, 6, 1.5*math.sin(angle))
				sim.partProperty(i, 1, 100+math.random()*60)
			end
		end
	elseif photId ~= nil and sim.partProperty(photId, 0) == 173 then
		if math.random(1,30) == 1 then
			sim.partProperty(i, 1, 300)
			sim.partProperty(i, 9, 1)
			newPhotId = sim.partCreate(-1, x, y, 173)
			if newPhotId >= 0 then
				angle = math.atan2(sim.partProperty(photId, 6), sim.partProperty(photId, 5)) + math.random()*0.2-0.1
				sim.partProperty(newPhotId, 5, 1.5*math.cos(angle))
				sim.partProperty(newPhotId, 6, 1.5*math.sin(angle))
			end
		end
	end
end

tpt.element_func(rdac1_update, rdac1)

local move_l = elements.allocate('MY12345', 'MOVELEFT')
local move_r = elements.allocate('MY12345', 'MOVERIGHT')
elements.element(move_l, elements.element(elements.DEFAULT_PT_HEAC))
elements.element(move_r, elements.element(elements.DEFAULT_PT_HEAC))
elements.property(move_l, 'Name', "MOVL")
elements.property(move_r, 'Name', "MOVR")
elements.property(move_l, "Description", "")
elements.property(move_r, "Description", "")
elements.property(move_l, "MenuSection", elem.SC_SPECIAL)
elements.property(move_r, "MenuSection", elem.SC_SPECIAL)
elements.property(move_l, "Update", (function (i, x, y, ss, nt)
	local pmap1 = sim.pmap(x, y-1)
	local pmap2 = sim.pmap(x-1, y-1)
	if pmap1 ~= nil then
		if sim.partProperty(pmap1, 5) > -1 then
			sim.partProperty(pmap1, 5, -1)
		end
		if pmap2 ~= nil then
			sim.partProperty(pmap1, 6, -1)
		end
	end
	pmap1 = sim.pmap(x, y-2)
	if pmap1 ~= nil and sim.partProperty(pmap1, 5) > -1 then
		sim.partProperty(pmap1, 5, -1)
	end
end))

elements.property(move_r, "Update", (function (i, x, y, ss, nt)
	local pmap1 = sim.pmap(x, y-1)
	local pmap2 = sim.pmap(x+1, y-1)
	if pmap1 ~= nil then
		if sim.partProperty(pmap1, 5) < 1 then
			sim.partProperty(pmap1, 5, 1)
		end
		if pmap2 ~= nil then
			sim.partProperty(pmap1, 6, -1)
		end
	end
	pmap1 = sim.pmap(x, y-2)
	if pmap1 ~= nil and sim.partProperty(pmap1, 5) < 1 then
		sim.partProperty(pmap1, 5, 1)
	end
end))

local sevsegt = {
{1,1,1,1,1,1,0},
{0,1,1,0,0,0,0},
{1,1,0,1,1,0,1},
{1,1,1,1,0,0,1},
{0,1,1,0,0,1,1},
{1,0,1,1,0,1,1},
{1,0,1,1,1,1,1},
{1,1,1,0,0,0,0},
{1,1,1,1,1,1,1},
{1,1,1,1,0,1,1},
};

local function print7seg (x, y, d)
	local idir = {0,1,1,0,1,1,0}
	local iix = {0,2,2,0,-2,-2,0}
	local iiy = {-10,-8,-4,-2,-4,-8,-6}
	local var2 = d + 1
	for j = 1, 7 do
		old = sim.pmap(x+iix[j], y+iiy[j])
		new = sevsegt[var2][j] == 1
		dy = idir[j]
		dx = 1 - dy
		for k = -1, 1 do
			old2 = sim.pmap(x+iix[j]+k*dx, y+iiy[j]+k*dy)
			if new and not old2 then
				sim.partCreate(-1, x+iix[j]+k*dx, y+iiy[j]+k*dy, 38)
			elseif old2 and not new then
				sim.partKill(old2)
			end
		end
	end
end

elements.property(189, "Update", (function (i, x, y, ss, nt)
	local isevsegt = {{0,0},{1,1},{2,2},{3,3},{4,4},{1,0},{0,4},{6,7},{8,2},{9,3},{4,10},{5,11},{0,5},{1,13},{0,6},{1,15},{14,16},{3,17},{4,18},{0,2},{20,1},{2,21},{3,22},{7,0},{24,1},{25,2},{0,3},{0,9},{27,28},{0,8},{1,30},{29,31},{26,32},{23,33},{19,34},{12,35}}
	local var2, j, old, new, dx, dy, old2, time, timetab
	if sim.partProperty(i, 1) == 39 then
		if sim.partProperty(i, "tmp") <= 0 then
			if tpt.get_property("type", x, y+1) ~= 15 or tpt.get_property("life", x, y+1) ~= 3 then
			return end
		elseif sim.partProperty(i, "ctype") <= 0 then
			sim.partProperty(i, "ctype", sim.partProperty(i, "tmp") - 1)
		else
			sim.partProperty(i, "ctype", sim.partProperty(i, "ctype") - 1)
			return 0
		end
		repeat
			local var = 36
			var = ((sim.pmap(x, y-10) == nil) and isevsegt[var][1] or isevsegt[var][2])
			var = ((sim.pmap(x+2, y-8) == nil) and isevsegt[var][1] or isevsegt[var][2])
			var = ((sim.pmap(x+2, y-4) == nil) and isevsegt[var][1] or isevsegt[var][2])
			var = ((sim.pmap(x, y-2) == nil) and isevsegt[var][1] or isevsegt[var][2])
			var = ((sim.pmap(x-2, y-4) == nil) and isevsegt[var][1] or isevsegt[var][2])
			var = ((sim.pmap(x-2, y-8) == nil) and isevsegt[var][1] or isevsegt[var][2])
			var = ((sim.pmap(x, y-6) == nil) and isevsegt[var][1] or isevsegt[var][2])
			var2 = (var + 1) % 10
			print7seg(x, y, var2)
			x = x - 6
		until var ~= 9
	elseif sim.partProperty(i, 1) == 40 then
		time = os.time()
		timetab = os.date("*t", time)
		print7seg(x, y, timetab.sec % 10)
		print7seg(x-  6, y, math.floor(timetab.sec / 10))
		print7seg(x- 16, y, timetab.min % 10)
		print7seg(x- 22, y, math.floor(timetab.min / 10))
		print7seg(x- 32, y, timetab.hour % 10)
		print7seg(x- 38, y, math.floor(timetab.hour / 10))
		if (sim.partProperty(i, "ctype") == 0) then
			sim.partProperty(i, "ctype", 39)
			sim.partProperty(i, "tmp", (sim.partProperty(i, "tmp") + 1) % 2)
			if sim.partProperty(i, "tmp") == 1 then
				sim.partCreate(-1, x-11, y-8, 38)
				sim.partCreate(-1, x-11, y-4, 38)
				sim.partCreate(-1, x-27, y-8, 38)
				sim.partCreate(-1, x-27, y-4, 38)
			else
				sim.partKill(x-11, y-8)
				sim.partKill(x-11, y-4)
				sim.partKill(x-27, y-8)
				sim.partKill(x-27, y-4)
			end
		else
			sim.partProperty(i, "ctype", sim.partProperty(i, "ctype") - 1)
		end
	elseif sim.partProperty(i, 1) == 41 then
		time = os.time()
		timetab = os.date("*t", time)
		print7seg(x, y, timetab.day % 10)
		if timetab.day >= 10 then
			print7seg(x-6, y, math.floor(timetab.day / 10))
		end
		print7seg(x-18, y, timetab.month % 10)
		if timetab.month >= 10 then
			print7seg(x-24, y, math.floor(timetab.month / 10))
		end
		print7seg(x-36, y, timetab.year % 10)
		print7seg(x-42, y, math.floor(timetab.year / 10) % 10)
		print7seg(x-48, y, math.floor(timetab.year / 100) % 10)
		print7seg(x-54, y, math.floor(timetab.year / 1000))
		sim.partCreate(-1, x-10, y-6, 38)
		sim.partCreate(-1, x-11, y-6, 38)
		sim.partCreate(-1, x-12, y-6, 38)
		sim.partCreate(-1, x-13, y-6, 38)
		sim.partCreate(-1, x-14, y-6, 38)
		sim.partCreate(-1, x-28, y-6, 38)
		sim.partCreate(-1, x-29, y-6, 38)
		sim.partCreate(-1, x-30, y-6, 38)
		sim.partCreate(-1, x-31, y-6, 38)
		sim.partCreate(-1, x-32, y-6, 38)
	elseif sim.partProperty(i, 1) == 42 then
		local j, nx, ny
		for j, nx, ny in sim.neighbors(x,y,1,1) do
			if sim.partProperty(j, 0) == 15 and sim.partProperty(j, 1) == 3 then
				sim.loadSave(sim.partProperty(i, "ctype"))
				return
			end
		end
	end
end))

local otwi = elements.allocate('MY12345', 'ONETIMEWIRE')
elements.element(otwi, elements.element(elements.DEFAULT_PT_METL))
elements.property(otwi, 'Name', "OTWR")
elements.property(otwi, "Description", "One time wire")
elements.property(otwi, "Properties", elem.TYPE_SOLID + elem.PROP_CONDUCTS + elem.PROP_LIFE_DEC + elem.PROP_LIFE_KILL_DEC)
elements.property(otwi, "Color", 0x601050)

local airsens = elements.allocate('MY12345', 'AIRSENSOR')
elements.element(airsens, elements.element(elements.DEFAULT_PT_DTEC))
elements.property(airsens, 'Name', "ASNS")
elements.property(airsens, "Description", "Air sensor")
elements.property(airsens, "Color", 0xB0FF00)
elements.property(airsens, "Temperature", 283.15)
elements.property(airsens, "Update", (function (i, x, y, ss, nt)
	local pavg0 = sim.partProperty(i, sim.FIELD_PAVG0)
	local pavg1 = sim.pressure(x/4, y/4)
	local partprop = sim.partProperty
	local mindiff = partprop(i, sim.FIELD_TEMP)-273.15
	mindiff = mindiff * ((mindiff > 0) and 0.1 or 0)
	partprop(i, sim.FIELD_PAVG0, pavg1)
	if (math.abs(pavg0-pavg1) > mindiff) then
		for j, nx, ny in sim.neighbors(x,y,1,1) do
			sim.partCreate(j, nx, ny, 15)
		end
	end
end))


local airsens2 = elements.allocate('MY12345', 'AIRSENSOR2')
elements.element(airsens2, elements.element(elements.DEFAULT_PT_DTEC))
elements.property(airsens2, 'Name', "AVS")
elements.property(airsens2, "Description", "Air velocity sensor")
elements.property(airsens2, "Color", 0xB0B030)
elements.property(airsens2, "Temperature", 283.15)
elements.property(airsens2, "Update", (function (i, x, y, ss, nt)
	if sim.velocityX(x/4, y/4) ^ 2 + sim.velocityY(x/4, y/4) ^ 2 > ((sim.partProperty(i, sim.FIELD_TEMP) - 273.15) * 0.1) ^ 2 then
		for j, nx, ny in sim.neighbors(x,y,1,1) do
			sim.partCreate(j, nx, ny, 15)
		end
	end
end))

local prdetect = elements.allocate('MY12345', 'PRESSURERANGEDETECTOR')
elements.element(prdetect, elements.element(elements.DEFAULT_PT_DTEC))
elements.property(prdetect, 'Name', "PRRD")
elements.property(prdetect, "Description", "Pressure range detector")
elements.property(prdetect, "Color", 0xB000FF)
elements.property(prdetect, "Update", (function (i, x, y, ss, nt)
	local partprop = sim.partProperty
	local pressure = sim.pressure(x/4, y/4)
	if partprop(i, sim.FIELD_TMP) >= pressure and partprop(i, sim.FIELD_TMP2) <= pressure then
		for j, nx, ny in sim.neighbors(x,y,1,1) do
			sim.partCreate(j, nx, ny, 15)
		end
	end
end))

local e2vibr = elements.allocate('MY12345', 'EXOT2VIBR')
elements.element(e2vibr, elements.element(elements.DEFAULT_PT_EXOT))
elements.property(e2vibr, 'Name', "EXO2")
elements.property(e2vibr, "Description", "converts EXOT to VIBR")
elements.property(e2vibr, "Properties", elem.TYPE_LIQUID + elem.PROP_LIFE_DEC)
elements.property(e2vibr, "Update", (function (i, x, y, ss, nt)
	local pp = sim.partProperty
	local life = pp(i, 1)
	for j, nx, ny in sim.neighbors(x,y,1,1) do
		if pp(j, 0) == 135 or pp(j, 0) == 145 then
			sim.partChangeType(j, e2vibr)
			pp(j, 1, 10)
			-- if life == 0 then pp(i, 1, 10) end
		end
	end
	if life == 1 then
		sim.partChangeType(i, 165); pp(i, 1, 0); pp(i, 9, 0); pp(i, 10, 0)
	end
end))

local melt2lava = elements.allocate('MY12345', 'MELT2LAVA')
elements.element(melt2lava, elements.element(elements.DEFAULT_PT_LAVA))
elements.property(melt2lava, 'Name', "MELT")
elements.property(melt2lava, "Description", "Melting something into LAVA")
elements.property(melt2lava, "Properties", elem.TYPE_LIQUID + elem.PROP_LIFE_DEC)
elements.property(melt2lava, "Temperature", sim.MAX_TEMP)
elements.property(melt2lava, "Hardness", 0)
elements.property(melt2lava, "Update", (function (i, x, y, ss, nt)
	local pp, pct = sim.partProperty, sim.partChangeType
	local life = pp(i, 1)
	local nty, c
	c = false
	for j, nx, ny in sim.neighbors(x,y,1,1) do
		nty = pp(j, 0)
		if elem.isDestructible(nty) and nty ~= melt2lava then
			c = true
			if nty ~= 6 then
				pp(j, 1, 10); pct(j, melt2lava); pp(j, 2, nty)
			end
		end
	end
	if life > 0 then
		if life == 1 then
			pct(i, 6)
			pp(i, 7, 10000)
			return
		end
		pp(i, 5, 0); pp(i, 6, 0)
	elseif c then
		sim.partKill(i)
	end
end))

local freeze2 = elements.allocate('MY12345', 'FREEZE2ICE')
elements.element(freeze2, elements.element(elements.DEFAULT_PT_SNOW))
elements.property(freeze2, 'Name', "FRZ2")
elements.property(freeze2, "Description", "Freezing something into ICE")
elements.property(freeze2, "Properties", elem.TYPE_POWDER)
elements.property(freeze2, "Temperature", 0)
elements.property(freeze2, "Hardness", 0)
elements.property(freeze2, "Update", (function (i, x, y, ss, nt)
	local pp, pct = sim.partProperty, sim.partChangeType
	local life = pp(i, 1)
	local nty, c
	c = false
	if life ~= 1 then
		for j, nx, ny in sim.neighbors(x,y,1,1) do
			nty = pp(j, 0)
			if elem.isDestructible(nty) and nty ~= freeze2 then
				c = true
				if nty ~= 13 then
					pp(j, 1, 12); pct(j, freeze2)
					if nty ~= 16 then pp(j, 2, nty) end
				end
			end
		end
		if life ~= 0 then
			pp(i, 1, life-1)
			pp(i, 5, 0); pp(i, 6, 0); pp(i, 7, 0)
		elseif c then
			sim.partKill(i)
		end
	elseif pp(i, 7) < 0.001 and math.random(1,100) == 1 then
		pct(i, 13); pp(i, 1, 0)
	else
		 pp(i, 5, 0); pp(i, 6, 0); pp(i, 7, 0)
	end
end))

local rebomb = elements.allocate('MY12345', 'REUSABLEBOMB')
elements.element(rebomb, elements.element(elements.DEFAULT_PT_PCLN))
elements.property(rebomb, 'Name', "RBMB")
elements.property(rebomb, "Description", "Partial of reusable bomb")
elements.property(rebomb, "Properties", elem.PROP_DRAWONCTYPE + elements.property(rebomb, "Properties"))
elements.property(rebomb, "Update", (function (i, x, y, ss, nt)
	local pp, pcr = sim.partProperty, sim.partCreate
	local r
	tpt.two_state_update(i,x,y,rebomb)
	if pp(i, 1) == 10 then
		r = sim.partCreate(-1,x+math.random(-1,1),y+math.random(-1,1),93)
		if r > 0 then
			pp(r, 2, pp(i, 2))
		end
	end
end))
elements.property(rebomb, "Graphics", (function (i, r, g, b)
	local l = sim.partProperty(i, 1) * 10
	return 0, 1, 255, r+l, g+l, b
end))

local p_airb = elements.allocate('MY12345', 'POWEREDAIRBLOCKER')
elements.element(p_airb, elements.element(elements.DEFAULT_PT_HSWC))
elements.property(p_airb, 'Name', "PAB")
elements.property(p_airb, 'Color', 0x605868)
elements.property(p_airb, "Description", "Powered air blocker. PSCN allows, NSCN blocks.")
elements.property(p_airb, "Update", (function (i, x, y, ss, nt)
	local pp, pcr = sim.partProperty, sim.partCreate
	local r
	tpt.two_state_update(i,x,y,p_airb)
	if pp(i, 1) < 10 then sim.blockAir(x/4, y/4, 2) end
end))
elements.property(p_airb, "Graphics", (function (i, r, g, b)
	local l = sim.partProperty(i, 1) * 10
	return 0, 1, 255, r+l, g+l, b+l
end))

local positron = elements.allocate('MY12345', 'POSITRON')
elements.element(positron, elements.element(elements.DEFAULT_PT_ELEC))
elements.property(positron, 'Name', "POSI")
elements.property(positron, "Description", "Positrons. reacts with ELEC")
elements.property(positron, "Update", (function (i, x, y, ss, nt)
	j = sim.partNeighbours(x, y, 2, 136)[0]
	local prop = sim.partProperty
	local angle = math.random()*6.2831853071796
	if prop(i, 9) == 0 then
		local vx, vy = 2*math.cos(angle), 2*math.sin(angle)
		prop(i, 9, 1)
		prop(i, 1, 680)
		prop(i, 5, vx)
		prop(i, 6, vy)
	end
	if j ~= nil then
		local vx, vy = 3*math.cos(angle), 3*math.sin(angle)
		sim.partChangeType(i, 31)
		sim.partChangeType(j, 31)
		prop(i, 2, 0x3FFFFFFF)
		prop(j, 2, 0x3FFFFFFF)
		prop(i, 5,  vx)
		prop(j, 5, -vx)
		prop(i, 6,  vy)
		prop(j, 6, -vy)
	end
end))

--[[
for ry = 0, 15 do
	for rx = 0, 15 do
		sim.partCreate2(-1, rx+ 80, ry+100, 189, 35,  rx+ry*16)
		sim.partCreate2(-1, rx+100, ry+100, 189, 35, (rx+ry*16)*256+ 78)
		sim.partCreate2(-1, rx+120, ry+100, 189, 35, (rx+ry*16)*256+189)
	end
end
]]